package com.woniu.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.woniu.common.entity.Result;
import com.woniu.pay.entity.PayInfo;
import com.woniu.pay.entity.RefundInfo;
import com.woniu.pay.service.AliPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/alipay")
@Api(tags = "支付服务")
@Slf4j
public class AliPayController {

    @Autowired
    private AliPayService alipayService;



    @PostMapping("/pay")
    @ApiOperation("支付方法")
    public Result pay(@RequestBody PayInfo payInfo) throws AlipayApiException {
        //支付
        String form = alipayService.pay(payInfo);

        //定时任务解决异步没有回来的问题 TODO
        return Result.ok(form);
    }

    @PostMapping("/check/{outTradeNo}")
    @ApiOperation("根据订单号查询支付状态")
    public Result queryStatusByOutTradeNo(@PathVariable String outTradeNo){
        AlipayTradeQueryResponse response = alipayService.queryStatusByOutTradeNo(outTradeNo);
        return Result.ok(response);
    }

    @PostMapping("/refund")
    @ApiOperation("支付宝退单")
    public Result refund(@RequestBody RefundInfo refundInfo){
        AlipayTradeRefundResponse response = alipayService.refund(refundInfo);
        return Result.ok(response);
    }




}
